﻿var ResidentDetail = function () {
    var initData = {};

    var ctr =  $('.mnResident');

    var initMenu = function () {
        var menu = ctr.find('#mainmenu');
        var selectMenu = ctr.find('#selectMenu');
        selectMenu.change(function () {
            var resident_id = this.value;
            menu.find('.menuitem').removeClass("active");
            menu.find('.menuitem[resident_id=' + resident_id + ']').addClass("active");
            loadForm(resident_id);
        });

        menu.setMask("Tải thành viên...");
        $.ajax({
            url: initData.getResidentList,
            dataType : "json",
            success: function (data) {
                $(data).each(function () {
                    var me = this;
                    var menuItem = $(ctr.find("#tplMenu").html());
                    menuItem.attr('resident_id', me.RESIDENT_ID)
                    menuItem.find(".title").html(me.FULL_NAME);

                    menuItem.click(function () {
                        menu.find('.menuitem').removeClass("active");
                        $(this).addClass("active");
                        var resident_id = $(this).attr('resident_id');
                        selectMenu.val(resident_id);
                        $('#thongtin').text("THÔNG TIN CÁ NHÂN");
                        loadForm(resident_id);
                    });
                    menu.append(menuItem);

                    var option = $("<option></option>");
                    option.attr('value', me.RESIDENT_ID);
                    option.html(me.FULL_NAME);
                    selectMenu.append(option);
                });

                menu.unMask();

                //$.each(data, function (key, data) {
                //    alert(key);
                //})
            },
            error: function () {
                menu.setMask("Lỗi kết nối");
            }
        })
    };


    var loadForm = function (resident_id) {
        ctr.find('#mainForm').setMask("Đang tải...");
        $.ajax({
            url: initData.getThongTinCN,
            type:'get',
            dataType: 'html',
            data: { residentId: resident_id },
            headers: {VerificationToken: initData.token},
            success: function (html) {
                ctr.find('#mainForm').unMask();
                ctr.find('#mainForm').html(html);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                ctr.find('#mainForm').setMask('Có lỗi xảy ra trong quá trình đăng nhập: ' + errorThrown);
            }
        });
    };

    return {
        init: function (initD) {
            initData = initD;
            initMenu();
        },
        loadForm: loadForm
    }
}();